package ru.urfc.portal.ui.client.renderers;

import java.util.ArrayList;
import java.util.List;

import ru.urfc.portal.ui.client.descriptor.CompositeGridField;
import ru.urfc.portal.ui.client.descriptor.GridField;

import com.extjs.gxt.ui.client.data.ModelData;
import com.google.gwt.dom.client.Element;

public class CompositeRenderer extends SimpleRenderer {
	
	private String starting = "";
	private String separator = " ";
	private String ending = "";
	private List<SimpleRenderer> renderers = new ArrayList<SimpleRenderer>();

	public CompositeRenderer(CompositeGridField field, Element element) {
		super(field, element);
		for (GridField subField: field.getGridFields()) {
			renderers.add(new TextRenderer(subField, element));
		}
	}

	public void init(String starting, String separator, String ending) {
		this.starting = starting;
		this.separator = separator;
		this.ending = ending;
	}
	
	@Override
	public String render(ModelData modelData) {
		String result = starting;
		boolean needSeparator = false;
		for (SimpleRenderer renderer: renderers) {
				if (renderer.getField().getValue(modelData) == null) {
					continue;
				}
				if (needSeparator) {
					result += separator;
				}
				result += renderer.render(modelData);
				needSeparator = true;
		}
		return result + ending;
	}

}
